package com.study.jyg.rxjava2study.activity.wifidevicesearch;

import android.os.Handler;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;

public class WifiDeviceSearchActivity extends BasicActivity implements View.OnClickListener {

    private TextView logContainer;
    private SearchThread searchThread;
    private ResponseThread responseThread;
    private boolean in_searching, in_response;


    @Override
    protected int getLayout() {
        return R.layout.activity_wifi_device_search;
    }

    @Override
    protected void initView() {
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
        findViewById(R.id.online).setOnClickListener(this);
        findViewById(R.id.offline).setOnClickListener(this);
        logContainer = findViewById(R.id.log);
    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            showLog((String) msg.obj);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                if (!in_searching) {
                    searchThread = new SearchThread(mHandler, 20);
                    searchThread.startSearch();
                    in_searching = true;
                } else {
                    Toast.makeText(this, "线程已经启动", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.stop:
                if (in_searching) {
                    searchThread.stopSearch();
                    in_searching = false;
                } else {
                    Toast.makeText(this, "线程未启动", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.online:
                if (!in_response) {
                    responseThread = new ResponseThread(mHandler);
                    responseThread.startResponse();
                    in_response = true;
                } else {
                    Toast.makeText(this, "线程已经启动", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.offline:
                if (in_response) {
                    responseThread.stopResponse();
                    in_response = false;
                } else {
                    Toast.makeText(this, "线程未启动", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void showLog(final String msg) {
        String text = logContainer.getText().toString();
        if (TextUtils.isEmpty(text))
            logContainer.setText(msg);
        else
            logContainer.setText(text+"\n"+msg);
    }

}